<?php
/**
 * 用户中心 - 3G网站相关
 * 
 * @author liulong, yangdan
 * @copyright 1wedo
 */
class PostCoreController extends BackendController {
    public $userid;
    public $user_token;
    public $cs;
    public $assetsUrl;
    public function init(){
        parent::init();
        $this->userid = Yii::app()->admin->getState('user_id');
        $this->user_token = Yii::app()->admin->getState('user_token');
        $this->cs = Yii::app()->clientScript;
        $this->assetsUrl = $this->getModule()->assetsUrl;
    }

    /**
     * 内容页所需要的公共js CSS
     */
    protected function getStatic(){
        $this->cs->registerScriptFile($this->assetsUrl . '/js/jquery.min.js',CClientScript::POS_HEAD);
        $this->cs->registerScriptFile($this->assetsUrl . '/js/jquery.cookie.js',CClientScript::POS_HEAD);
        $this->cs->registerScriptFile($this->assetsUrl . '/js/jquery.treeview.js',CClientScript::POS_HEAD);
        $this->cs->registerCssFile($this->assetsUrl . '/css/jquery.treeview.css');
    }

    /**
     * @param $userid
     * @return array|string
     * 内容页的左侧树结构
     */
    protected function getTree($userid){
        $category_info = Category::model()->categoryInfo($userid);
        Yii::import('ext.common.tree');
        $tree = new tree();
        $cateinfo = array();
        if (!empty($category_info)) {
            foreach ($category_info as $r) {
                //单页面
                if($r['type']=='page'){
                    $r['vs_show'] ='';
                    $r['icon_type'] = '';
                    $r['add_icon'] = "<a href='".Yii::app()->createUrl('user/site/cPage',array('id'=>$r['id']))."' >".CHtml::image($this->assetsUrl.'/img/tree/file.gif')."</a> ";
                    $r['xurl'] = Yii::app()->createUrl('user/site/cPage',array('id'=>$r['id']));
                    $r['display_type'] = 'add';
                }elseif($r['type']=='use'){
                    $r['vs_show'] ='';
                    $r['icon_type'] = '';
                    $r['add_icon'] = "<a href='".Yii::app()->createUrl('user/site/cApp',array('id'=>$r['id']))."' >".CHtml::image($this->assetsUrl.'/img/tree/full-screen-alt-5.png')."</a> ";
                    $r['xurl'] = Yii::app()->createUrl('user/site/cApp',array('id'=>$r['id']));
                    $r['display_type'] = 'add';
                }else{
                    $r['icon_type'] = $r['vs_show'] = '';
                    $r['display_type'] = 'init';
                    $r['xurl'] = Yii::app()->createUrl('user/site/cList',array('id'=>$r['id']));
                    $r['add_icon'] = "<a href='".Yii::app()->createUrl('user/site/cList',array('id'=>$r['id']))."' >".CHtml::image($this->assetsUrl.'/img/notepad-alt.png')."</a> ";
                }
                $cateinfo[$r['id']] = $r;
            }

        }
        if (!empty($cateinfo)) {
            $tree->init($cateinfo);
            $strs = "<span class='\$icon_type'>\$add_icon <a href='\$xurl'  >\$name</a></span>";
            $strs2 = "<span class='folder'>0</span>\$name";
            $cateinfo = $tree->get_treeview(0,'category_tree',$strs,$strs2,0);
        }
        return $cateinfo;
    }
    protected function checkInfo($template_id,$model_id,$category_id){
        $template_info = Template::model()->findByPk($template_id); //套餐参数数据
        $tem_arr = unserialize($template_info->show_model);  //套餐模型的数组
        if(!in_array($model_id,$tem_arr)){ //用户没有操作此模型的权利
            $this->redirect_message('您无权操作!','error',1,Yii::app()->createUrl('user/site/post'));
        }
        $category_info = Category::model()->checkId($category_id,$this->userid);
        if(!$category_info){
            $this->redirect_message('您无权操作!','error',1,Yii::app()->createUrl('user/site/post'));
        }
        //不为列表页面
        if($category_info->type !='list'){
            $this->redirect_message('非法操作!','error',1,Yii::app()->createUrl('user/site/post'));
        }
        return $category_info;
    }

}